package com.molichuxing.services.infrastructure.service.impl;

import javax.annotation.Resource;

import com.moli.iov.util.Md5PwdEncoder;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.services.infrastructure.dto.response.response.AccountCreateDto;
import com.molichuxing.services.infrastructure.dto.response.response.AccountModifyDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.exception.PermissionExceptionCodeEnum;
import com.molichuxing.services.infrastructure.bean.AccountBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.AccountMapper;
import com.molichuxing.services.infrastructure.dto.response.response.AccountDto;
import com.molichuxing.services.infrastructure.service.AccountService;
import com.molichuxing.services.property.SystemTypeEnum;
import org.springframework.util.Base64Utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账户基础服务
 * 
 * @author zoumingyu
 * @date 2019年9月9日
 *
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {
	private static final Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);


	@Resource
	private AccountMapper accountMapper;


	
    @Override
    public AccountDto getEffectiveAccount(String account, String password, SystemTypeEnum systemType) throws BizException {
		AccountBean accountBean = accountMapper.getAccount(account,
				systemType == null ? null : systemType.getValue());

		if (accountBean == null) {
			// 账户不存在
			throw new BizException(PermissionExceptionCodeEnum.NOT_FOUND.getCode(),
					PermissionExceptionCodeEnum.NOT_FOUND.getMessage());
		} else if (accountBean.getIsDeleted()) {
			// 账户已删除
			throw new BizException(PermissionExceptionCodeEnum.DELETE.getCode(),
					PermissionExceptionCodeEnum.DELETE.getMessage());
		} else if (accountBean.getStatus() == 1) {
			// 账户已禁用
			throw new BizException(PermissionExceptionCodeEnum.DISABLE.getCode(),
					PermissionExceptionCodeEnum.DISABLE.getMessage());
		} else {
			password = Md5PwdEncoder.encodePassword(new String(Base64Utils.decodeFromString(password)), accountBean.getPasswordSalt());
			if (!password.equals(accountBean.getPassword())) {
				// 不正确
				throw new BizException(PermissionExceptionCodeEnum.PWD_ERROR.getCode(),
						PermissionExceptionCodeEnum.PWD_ERROR.getMessage());
			}
		}

		return Convert.toAccountDto(accountBean);
    }

	@Override
	public AccountDto getEffectiveAccountByIov(String account, String password, SystemTypeEnum systemType) throws BizException {
		AccountBean accountBean = accountMapper.getAccount(account,
				systemType == null ? null : systemType.getValue());

		if (accountBean == null) {
			// 账户不存在
			throw new BizException(PermissionExceptionCodeEnum.NOT_FOUND.getCode(),
					PermissionExceptionCodeEnum.NOT_FOUND.getMessage());
		} else if (accountBean.getIsDeleted()) {
			// 账户已删除
			throw new BizException(PermissionExceptionCodeEnum.DELETE.getCode(),
					PermissionExceptionCodeEnum.DELETE.getMessage());
		} else if (accountBean.getStatus() == 1) {
			// 账户已禁用
			throw new BizException(PermissionExceptionCodeEnum.DISABLE.getCode(),
					PermissionExceptionCodeEnum.DISABLE.getMessage());
		} else {
			password = Md5PwdEncoder.encodePassword(password, accountBean.getPasswordSalt());
			if (!password.equals(accountBean.getPassword())) {
				// 不正确
				throw new BizException(PermissionExceptionCodeEnum.PWD_ERROR.getCode(),
						PermissionExceptionCodeEnum.PWD_ERROR.getMessage());
			}
		}

		return Convert.toAccountDto(accountBean);
	}

	@Override
	public Paged<AccountDto> getAccountList(Map<String, Object> param, int pageNum, int pageSize) {
		int count = accountMapper.getAccountCount(param);
		if (count > 0) {
			List<AccountBean> beanList = accountMapper.getAccountList(param, pageNum, pageSize);
			Paged<AccountDto> result = new Paged<>(Convert.toAccountDtoList(beanList), count, pageSize);
			return result;
		}
		return new Paged<>(null,0,pageSize);
	}

	@Override
	public int getAccountCount(Map<String, Object> param) {
		return accountMapper.getAccountCount(param);
	}

	@Override
	public int createAccount(AccountCreateDto dto) {
		if (dto != null) {
			AccountBean bean = Convert.toAccountBean(dto);
			int row = accountMapper.createAccount(bean);
			if (row > 0) {
				return bean.getId();
			}
		}
		return 0;
	}

	@Override
	public int modifyAccount(AccountModifyDto dto) {
		return accountMapper.modifyAccount(Convert.toAccountBean(dto));
	}

    @Override
    public int modifyAccount(AccountDto dto) {
		return accountMapper.modifyAccount(Convert.toAccountBean(dto));
    }

    @Override
	public AccountDto getById(int id) {
		if (id > 0){
			return Convert.toAccountDto(accountMapper.getById(id));
		}
		return null;
	}

	@Override
	public int modifyPassword(String password, int id) {
		return accountMapper.modifyPassword(password, id);
	}

	@Override
	public int modifyPasswordByIov(String password, String account) {
		return accountMapper.modifyPasswordByIov(password, account);
	}

    @Override
    public AccountDto getIovAccount() {
		return Convert.toAccountDto(accountMapper.getIovAccount());
    }

	@Override
	public int modifyAccountByIov(AccountDto dto) {
		if (dto == null) {
			throw new IllegalArgumentException("dto is null");
		}
		return accountMapper.modifyAccountByIov(Convert.toAccountBean(dto));
	}

	@Override
	public List<AccountDto> getAccountByParam(Map<String, Object> param) {
		return Convert.toAccountDtoList(accountMapper.getAccountByParam(param));
	}

	@Override
	public AccountDto getAccount(String account, int systemType) {
		return Convert.toAccountDto(accountMapper.getAccount(account, systemType));
	}

    @Override
    public int modifyAccountToDisable(List<Integer> ids) {
    	return accountMapper.modifyAccountToDisable(ids);
    }

	@Override
	public int modifyAccountStatusToOpen(List<Integer> ids) {
		return accountMapper.modifyAccountStatusToOpen(ids);
	}
}
